package com.example.mysimpleexample.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.mysimpleexample.R;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageFragment;


import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

@Page(name = "网络图片下载转为Bitmap")
public class DownloadPictureToBitmapFragment extends XPageFragment {
    @BindView(R.id.etPictureUrl)
    EditText etPictureUrl;
    @BindView(R.id.btnDownload)
    Button btnDownload;
    @BindView(R.id.imageView)
    ImageView imageView;
    private static OkHttpClient okHttpClient;

    static {
        okHttpClient = new OkHttpClient();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_download_picture_to_bitmap;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {

    }

    @OnClick(R.id.btnDownload)
    protected void onViewClicked(View view) {
        /*String downloadUrl = etPictureUrl.getText().toString();*/
        String downloadUrl = "https://static.oschina.net/uploads/space/2021/0318/072757_XrsZ_2744687.png";
        Observable.just(downloadUrl)
                .map(new Function<String, Bitmap>() {
                    @Override
                    public Bitmap apply(@NonNull String s) throws Exception {
                        Request request = new Request.Builder()
                                .url(downloadUrl)
                                .build();
                        Bitmap bitmap = null;
                        Response response = okHttpClient.newCall(request).execute();
                        if (response.isSuccessful()) {
                            InputStream inputStream = response.body().byteStream();
                            bitmap = BitmapFactory.decodeStream(inputStream);
                            inputStream.close();
                        }
                        return bitmap;
                    }
                }).map(new Function<Bitmap, Bitmap>() {
            @Override
            public Bitmap apply(@NotNull Bitmap bitmap) throws Exception {
                bitmap = createWatermark(bitmap, "RxJava2.0");
                return bitmap;
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bitmap>() {
                    @Override
                    public void accept(Bitmap bitmap) throws Exception {
                        imageView.setImageBitmap(bitmap);
                    }
                });
    }

    private Bitmap createWatermark(Bitmap bitmap, String mark) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Bitmap bitmap1 = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        Paint paint = new Paint();
        paint.setColor(Color.parseColor("#C5FF0000"));
        paint.setTextSize(150);
        paint.setAntiAlias(true);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.drawText(mark, 0, h / 2, paint);
        canvas.save();
        canvas.restore();
        return bitmap1;
    }
}
